import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_lang/flutter_lang.dart';
import 'package:flutter_lifecycle_base/base_state_widget.dart';
import 'package:flutter_lifecycle_base/widget/status_bar_wrap.dart';
import 'package:provider/provider.dart';
import 'package:wallet_home/module_config.dart';
import 'package:wallet_home/page/theme/theme_provider.dart';
import 'package:wallet_home/tools/common.dart';
import 'package:wallet_home/tools/theme_utils.dart';

import 'colors.dart';

class ThemePage extends BaseStatefulWidget {
  @override
  _ThemePageState createState() {
    return _ThemePageState();
  }
}

class _ThemePageState extends BaseWidgetState<ThemePage> {
  final List<String> _list = [
    "${WXLang.$t("home_day_mode", package: ModuleConfig.moduleName)}",
    "${WXLang.$t("home_night_mode", package: ModuleConfig.moduleName)}",
    "${WXLang.$t("home_follow_system", package: ModuleConfig.moduleName)}"
  ];
  int position = 0;

  @override
  void initState() {
    super.initState();
    String themeType = SpUtil.getString(Constant.themeModel);
    switch (themeType) {
      case "light":
        position = 0;
        break;
      case "dark":
        position = 1;
        break;
      default:
        position = 2;
        break;
    }
  }

  @override
  void didAppear() {
    super.didAppear();
    print("ThemePage回来了");
  }

  @override
  void disappear() {
    super.disappear();
    print("ThemePage消失了");
  }

  @override
  Widget build(BuildContext context) {
    return StatusBarWrap(
//      style: ThemeUtils.isDark(context)
//          ? SystemUiOverlayStyle.light
//          : SystemUiOverlayStyle.dark,
      child: Scaffold(
          appBar: AppBar(
            title: Text(
              '${WXLang.$t("home_theme_selection", package: ModuleConfig.moduleName)}',
              style: TextStyle(
                  color: ThemeUtils.getThemeColor(
                      context: context,
                      brightnessColor: Colors.grey[200],
                      darkColor: Colors.white)),
            ),
            brightness: Brightness.dark,
            centerTitle: true,
            leading: IconButton(
                icon: Icon(
                  Icons.arrow_back,
                  color: ThemeUtils.getThemeColor(
                      context: context,
                      brightnessColor: Colors.grey[200],
                      darkColor: Colors.white),
                ),
                onPressed: () {
                  Navigator.pop(context);
                }),
          ),
          body: Container(
            width: double.infinity,
            height: double.infinity,
            color: ThemeUtils.isDark(context)
                ? Colours.dark_bg_color
                : Colors.white,
            child: Column(
              children: [
                // _themePageHeader(context),
                Expanded(
                    child: MediaQuery.removePadding(
                        removeTop: true,
                        context: context,
                        child: ListView.separated(
                          separatorBuilder: (_, index) => Divider(),
                          itemCount: _list.length,
                          itemBuilder: (_, index) {
                            return InkWell(
                              onTap: () {
                                position = index;
                                setState(() {
                                  String themeMode = index == 0
                                      ? "light"
                                      : (index == 1 ? "dark" : "system");
                                  SpUtil.putString(
                                      Constant.themeModel, themeMode);

                                  Provider.of<ThemeProvider>(context,
                                          listen: false)
                                      .setTheme(themeMode);
                                });
                              },
                              child: Container(
                                alignment: Alignment.centerLeft,
                                padding: const EdgeInsets.symmetric(
                                    horizontal: 16.0),
                                height: 40.0,
                                child: Row(
                                  children: <Widget>[
                                    Expanded(child: Text(_list[index])),
                                    Opacity(
                                      opacity: position == index ? 1 : 0,
                                      child: Icon(
                                        Icons.done,
                                        color: ThemeUtils.getThemeColor(
                                            context: context,
                                            brightnessColor: Colours.app_main,
                                            darkColor: Colors.white),
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            );
                          },
                        )))
              ],
            ),
          )),
    );
  }
}
